package com.ruifu.doc.controller;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.doc.dto.fold.DmFoldDTO;
import com.ruifu.doc.dto.fold.DmFoldUpdateDTO;
import com.ruifu.doc.service.DmFoldService;
import com.ruifu.doc.vo.fold.DmFoldVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件夹控制层
 *
 * @author nyr
 * @create 2019/11/11
 */
@RestController
@CrossOrigin
@RequestMapping("/dmFold")
public class DmFoldController {
    @Autowired
    private DmFoldService dmFoldService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 创建文件夹
     *
     * @param dmFoldDTO
     * @return
     */
    @PostMapping("/insert")
    public ResultVO insert(@RequestBody DmFoldDTO dmFoldDTO) {
        try {
            Long fdFoldId = dmFoldService.insert(dmFoldDTO, cloudUser);
            return new ResultVO(true, fdFoldId + "");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 修改文件夹
     *
     * @param dmFoldUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public ResultVO update(@RequestBody DmFoldUpdateDTO dmFoldUpdateDTO) {
        try {
            dmFoldService.update(dmFoldUpdateDTO, cloudUser);
            return new ResultVO(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 删除文件夹
     *
     * @param fdFoldId 文件夹id
     * @return
     */
    @GetMapping("/delete/{fdFoldId}/{signBoardDoc}")
    public ResultVO delete(@PathVariable Long fdFoldId,@PathVariable Integer signBoardDoc) {
        try {
            dmFoldService.delete(fdFoldId, cloudUser,signBoardDoc);
            return new ResultVO(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 页面加载查询文件夹列表
     * @param signBoardDoc
     * @return
     */
    @GetMapping("/findList/{signBoardDoc}")
    public List<DmFoldVO> findList(@PathVariable Integer signBoardDoc) {
        return dmFoldService.findList(cloudUser,signBoardDoc);
    }

    /**
     * 根据文件夹id查询文件夹信息
     *
     * @param fdFoldId 文件夹id
     * @return
     */
    @GetMapping("/findById/{fdFoldId}")
    public DmFoldVO findById(@PathVariable Long fdFoldId) {
        return dmFoldService.findById(fdFoldId, cloudUser);
    }

    /**
     * 根据用户id和文件夹id查询权限
     *
     * @param fdFoldId 文件夹id
     * @return
     */
    @GetMapping(value = "/findPermission/{fdFoldId}")
    public String findPermissionByUserIdAndPtId(@PathVariable("fdFoldId") Long fdFoldId) {
        return dmFoldService.findPermissionByUserIdAndFold(cloudUser, fdFoldId);
    }
}
